package com.cabd.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cabd.domain.User;
import com.cabd.service.UserService;
import com.cabd.service.impl.UserServiceImpl;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String jspPage = request.getParameter("method");
		if(jspPage.equals("login")) {
			login(request, response);
		}else if (jspPage.equals("register")) {
			register(request, response);
		}else if (jspPage.equals("quit")) {
			quit(request, response);
		}else {
			logout(request, response);
		}
	}

	private void logout(HttpServletRequest request, HttpServletResponse response) {
		
	}
	
	/**
	 * 用户注销
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void quit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath()+"/home");
	}
	/**
	 * 用户注册
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		String codeComeServer = ((String) session.getAttribute("verCode")).toLowerCase();
		String codeComeBrowser = request.getParameter("code").toLowerCase();
		if(!codeComeServer.equals(codeComeBrowser)) {
			request.setAttribute("codeErrorMsg", "验证码不正确");
			request.getRequestDispatcher("/Register.jsp").forward(request, response);
			return;
		}
		User user = new User();
		user.setId(request.getParameter("userid"));
		user.setPwd(request.getParameter("password"));
		UserService userService = new UserServiceImpl();
		userService.register(user);
		session.setAttribute("user", user);
		response.sendRedirect(request.getContextPath() + "/home");
		System.out.println(user.getId() + " " + "注册成功！");
		System.out.println(user.getId() + " " + "登录成功！");
	}

	/**
	 * 用户登陆
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User userComeBrowser = new User();
		String userid = request.getParameter("userid");
		String password = request.getParameter("password");
		userComeBrowser.setId(userid);
		userComeBrowser.setPwd(password);
		if(userid.equals("admin")&&password.equals("admin")) {
			User user = new User();
			user.setId("admin");
			user.setPwd("admin");
			request.getSession().setAttribute("user", user);
			response.sendRedirect(request.getContextPath()+"/admin");
			System.out.println("admin 登陆成功！");
			return;
		}
		
		UserService userService = new UserServiceImpl();
		User userComeDataBase = userService.login(userComeBrowser);
		if(userComeDataBase == null) {
			request.setAttribute("loginError", "用户名或密码不正确！");
			request.getRequestDispatcher("/Login.jsp").forward(request, response);
			System.out.println(userComeBrowser.getId() + " " + "登录失败！");
		}else {
			request.getSession().invalidate();
			request.getSession().setAttribute("user", userComeDataBase);
			response.sendRedirect(request.getContextPath()+"/home");
			System.out.println(userComeBrowser.getId() + " " + "登录成功！");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}